package com.icetec.iceintel.database;

import android.content.ContentValues;
import android.database.Cursor;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.ArrayList;

import com.icetec.iceintel.model.Alarm;

/**
 * Created by amo on 2015/1/27.
 * 闹钟数据库
 */
public class AlarmConfigSqlHelper {




    public static final String TABLE_ALARM = "alarm";
    public static final String TABLE_ALARM_ID = "id";
    public static final String TABLE_ALARM_ACTIVE = "alarm_active";
    public static final String TABLE_ALARM_TIME = "alarm_time";
    public static final String TABLE_ALARM_DAYS = "alarm_days";
    public static final String TABLE_ALARM_TYPE = "alarm_type";
    public static final String TABLE_ALARM_TONE = "alarm_tone";
    public static final String TABLE_ALARM_VIBRATE = "alarm_vibrate";
    public static final String TABLE_ALARM_NAME = "alarm_name";
    public final static String TABLE_ALARM_ISONCE="alarm_once";  //是否一次性的闹钟
    public final static String TABLE_ALARM_MAC="alram_mac";
    public final static String TABLE_ALRAM_ATTACH_PORT="alarm_port";


    private ApplicationSQLiteHelper applicationSQLiteHelper=ApplicationSQLiteHelper.sharedInstance();


        public final static String CREATE_TABLE = "create table if not exists "+TABLE_ALARM+" ("
            + TABLE_ALARM_ID
            + " INTEGER primary key autoincrement , "
            + TABLE_ALARM_ACTIVE
            + " INTEGER NOT NULL ,  "
            + TABLE_ALARM_TIME
            + " varchar(40),  "
            + TABLE_ALARM_DAYS
            + " BLOB NOT NULL,  "
            + TABLE_ALARM_TYPE
            + " varchar(20),  "
            + TABLE_ALARM_TONE
            + " varchar(20),  "
            + TABLE_ALARM_VIBRATE
            + " INTEGER NOT NULL ,  "
            + TABLE_ALARM_ISONCE
            + " INTEGER NOT NULL , "
            + TABLE_ALARM_MAC
            + " varchar(40),"
                + TABLE_ALARM_NAME
                + " varchar(20),  "
            + TABLE_ALRAM_ATTACH_PORT
            + " INTEGER NOT NULL "
            +")";


    public  int deleteEntry(Alarm alarm){
        return deleteEntry(alarm.getId());
    }

    public int deleteEntry(int id){
        return applicationSQLiteHelper.getWritableDatabase().delete(TABLE_ALARM, TABLE_ALARM_ID + "=" + id, null);
    }



    public int deleteActiveAlarm(){

        return applicationSQLiteHelper.getWritableDatabase().delete(TABLE_ALARM, TABLE_ALARM_ACTIVE + "= 1"  , null);

    }

    public int deleteAllAlarm(){

        return applicationSQLiteHelper.getWritableDatabase().delete(TABLE_ALARM, null  , null);

    }


    public  ArrayList<Alarm> getAllAlarm(Alarm.AlarmType alarmType) {
        ArrayList<Alarm> alarms = new ArrayList<Alarm>();

        String[] columns = new String[] {
                TABLE_ALARM_ID,
                TABLE_ALARM_ACTIVE,
                         TABLE_ALARM_TIME,
                         TABLE_ALARM_TYPE,
                         TABLE_ALARM_TONE,
                         TABLE_ALARM_VIBRATE,
                         TABLE_ALARM_NAME,
                         TABLE_ALARM_ISONCE,
                         TABLE_ALARM_DAYS,
                         TABLE_ALARM_MAC,
                         TABLE_ALRAM_ATTACH_PORT,
        };

        Cursor cursor =  applicationSQLiteHelper.getWritableDatabase().query(TABLE_ALARM, columns,TABLE_ALARM_TYPE+"="+alarmType.ordinal(), null, null, null,
                null);

        if (cursor.moveToFirst()) {

            do {

                Alarm alarm = new Alarm();
                alarm.setId(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ID)));
                alarm.setAlarmActive(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ACTIVE)) == 1);
                alarm.setAlarmTime(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_TIME)));
                byte[] repeatDaysBytes = cursor.getBlob(cursor.getColumnIndex(TABLE_ALARM_DAYS));

                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        repeatDaysBytes);
                try {
                    ObjectInputStream objectInputStream = new ObjectInputStream(
                            byteArrayInputStream);
                    Alarm.Day[] repeatDays;
                    Object object = objectInputStream.readObject();
                    if (object instanceof Alarm.Day[]) {
                        repeatDays = (Alarm.Day[]) object;
                        alarm.setDays(repeatDays);
                    }
                } catch (StreamCorruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                alarm.setAlarmType(Alarm.AlarmType.values()[cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_TYPE))]);
                alarm.setAlarmTonePath(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_TONE)));
                alarm.setVibrate(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_VIBRATE)) == 1);
                alarm.setAlarmName(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_NAME)));
                alarm.setAlarmMAC(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_MAC)));
                alarm.setAlarmPort(cursor.getInt(cursor.getColumnIndex(TABLE_ALRAM_ATTACH_PORT)));
                alarm.setIsOnce(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ISONCE))==1);

                alarms.add(alarm);

            } while (cursor.moveToNext());
        }
        cursor.close();
        return alarms;
    }

    public  ArrayList<Alarm> getAllAlarm() {
        ArrayList<Alarm> alarms = new ArrayList<Alarm>();

        String[] columns = new String[] {
                TABLE_ALARM_ID,
                TABLE_ALARM_ACTIVE,
                TABLE_ALARM_TIME,
                TABLE_ALARM_TYPE,
                TABLE_ALARM_TONE,
                TABLE_ALARM_VIBRATE,
                TABLE_ALARM_NAME,
                TABLE_ALARM_ISONCE,
                TABLE_ALARM_DAYS,
                TABLE_ALARM_MAC,
                TABLE_ALRAM_ATTACH_PORT,
        };

        Cursor cursor =  applicationSQLiteHelper.getWritableDatabase().query(TABLE_ALARM, columns,null, null, null, null,
                null);

        if (cursor.moveToFirst()) {

            do {

                Alarm alarm = new Alarm();
                alarm.setId(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ID)));
                alarm.setAlarmActive(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ACTIVE)) == 1);
                alarm.setAlarmTime(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_TIME)));
                byte[] repeatDaysBytes = cursor.getBlob(cursor.getColumnIndex(TABLE_ALARM_DAYS));

                ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(
                        repeatDaysBytes);
                try {
                    ObjectInputStream objectInputStream = new ObjectInputStream(
                            byteArrayInputStream);
                    Alarm.Day[] repeatDays;
                    Object object = objectInputStream.readObject();
                    if (object instanceof Alarm.Day[]) {
                        repeatDays = (Alarm.Day[]) object;
                        alarm.setDays(repeatDays);
                    }
                } catch (StreamCorruptedException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }

                alarm.setAlarmType(Alarm.AlarmType.values()[cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_TYPE))]);
                alarm.setAlarmTonePath(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_TONE)));
                alarm.setVibrate(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_VIBRATE)) == 1);
                alarm.setAlarmName(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_NAME)));
                alarm.setAlarmMAC(cursor.getString(cursor.getColumnIndex(TABLE_ALARM_MAC)));
                alarm.setAlarmPort(cursor.getInt(cursor.getColumnIndex(TABLE_ALRAM_ATTACH_PORT)));
                alarm.setIsOnce(cursor.getInt(cursor.getColumnIndex(TABLE_ALARM_ISONCE))==1);

                alarms.add(alarm);

            } while (cursor.moveToNext());
        }
        cursor.close();
        return alarms;
    }

    public Long addAlarm(Alarm alarm) {
        ContentValues cv = new ContentValues();
        cv.put(TABLE_ALARM_ACTIVE, alarm.getAlarmActive());
        cv.put(TABLE_ALARM_TIME, alarm.getAlarmTimeString());
        cv.put(TABLE_ALARM_TYPE, alarm.getAlarmType().ordinal());
        cv.put(TABLE_ALARM_TONE, alarm.getAlarmTonePath());
        cv.put(TABLE_ALARM_VIBRATE, alarm.getVibrate());
        cv.put(TABLE_ALARM_NAME, alarm.getAlarmName());
        cv.put(TABLE_ALARM_ISONCE,alarm.getIsOnce());
        cv.put(TABLE_ALRAM_ATTACH_PORT,alarm.getAlarmPort());
        cv.put(TABLE_ALARM_MAC,alarm.getAlarmMAC());

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos =  new ObjectOutputStream(bos);
            oos.writeObject(alarm.getDays());
            byte[] buff = bos.toByteArray();
            cv.put(TABLE_ALARM_DAYS, buff);

        } catch (Exception e){
        }
        return applicationSQLiteHelper.getWritableDatabase().insert(TABLE_ALARM, null, cv);
    }

    public  int update(Alarm alarm) {
        ContentValues cv = new ContentValues();
        cv.put(TABLE_ALARM_ACTIVE, alarm.getAlarmActive());
        cv.put(TABLE_ALARM_TIME, alarm.getAlarmTimeString());
        cv.put(TABLE_ALARM_TYPE, alarm.getAlarmType().ordinal());
        cv.put(TABLE_ALARM_TONE, alarm.getAlarmTonePath());
        cv.put(TABLE_ALARM_VIBRATE, alarm.getVibrate());
        cv.put(TABLE_ALARM_NAME, alarm.getAlarmName());
        cv.put(TABLE_ALARM_ISONCE,alarm.getIsOnce());
        cv.put(TABLE_ALRAM_ATTACH_PORT,alarm.getAlarmPort());
        cv.put(TABLE_ALARM_MAC,alarm.getAlarmMAC());

        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream oos = null;
            oos = new ObjectOutputStream(bos);
            oos.writeObject(alarm.getDays());
            byte[] buff = bos.toByteArray();

            cv.put(TABLE_ALARM_DAYS, buff);

        } catch (Exception e){
        }

        return applicationSQLiteHelper.getWritableDatabase().update(TABLE_ALARM, cv, TABLE_ALARM_ID+"=" + alarm.getId(), null);
    }
}


